package educrypt.gui.ctr;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import educrypt.ciphers.Vigenere;
import educrypt.ciphers.hill.Hill;
import educrypt.ciphers.playfair.Playfair;
import educrypt.commons.Alphabet;
import educrypt.commons.Global;
import educrypt.database.dao.AlphabetDAO;
import educrypt.database.dao.KeywordDAO;
import educrypt.exceptions.EduCryptException;
import educrypt.gui.util.GuiUtil;
import educrypt.gui.view.EditorKeywordView;
import educrypt.gui.view.MainView;

public class EditorKeywordCtr implements ActionListener, ListSelectionListener {

	// View
	private EditorKeywordView editorKeywordView;

	// Model
	private KeywordDAO keywordDAO;

	public EditorKeywordCtr(MainView view) {
		this.editorKeywordView = view.getEditorKeywordDialog();

		keywordDAO = KeywordDAO.getInstance();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			String command = e.getActionCommand();
			if (command.equals(EditorKeywordView.CIPHER_SELECTED)) {
				this.showAlphabets();
				this.showStoredKeywords();
				editorKeywordView.enableAddButton(false);
			}
			if (command.equals(EditorKeywordView.ALPHABET_SELECTED)) {
				this.showStoredKeywords();
				editorKeywordView.enableAddButton(false);

			}
			if (command.equals(EditorKeywordView.VALIDATE_KEYWORD)) {
				this.validateKeyword();
			}
			if (command.equals(EditorKeywordView.ADD_KEYWORD)) {
				this.addKeyword();
				showStoredKeywords();
			}
			if (command.equals(EditorKeywordView.REMOVE_KEYWORD)) {
				this.removeKeyword();
				showStoredKeywords();
			}
		} catch (EduCryptException ex) {
			GuiUtil.showExceptionDialog((Component) editorKeywordView,
					ex.getMessage());
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		String selectedKeyword = editorKeywordView.getSelectedKeyword();
		editorKeywordView.setKeywordToEdit(selectedKeyword);
	}

	private void showAlphabets() {
		int selectedCipher = editorKeywordView.getSelectedCipher();
		List<Alphabet> alphabets = AlphabetDAO.getInstance().findByCipher(
				selectedCipher);
		List<String> alphabetsNames = new ArrayList<String>();
		for (Alphabet alphabet : alphabets) {
			alphabetsNames.add(alphabet.getName());
		}
		editorKeywordView.showAlphabets(alphabetsNames);
		if (alphabetsNames.isEmpty()) {
			editorKeywordView.enableButtons(false);
			editorKeywordView.hideKeywords();
		} else {
			editorKeywordView.enableButtons(true);
		}
	}

	private void showStoredKeywords() {
		int selectedCipher = editorKeywordView.getSelectedCipher();
		String selectedAlphabetName = editorKeywordView
				.getSelectedAlphabetName();
		List<String> keywords = keywordDAO.find(selectedCipher,
				selectedAlphabetName);
		editorKeywordView.showKeywords(keywords);
	}

	private void validateKeyword() throws EduCryptException {
		int selectedCipher = editorKeywordView.getSelectedCipher();

		String selectedAlphabetName = editorKeywordView
				.getSelectedAlphabetName();
		Alphabet selectedAlphabet = AlphabetDAO.getInstance().find(
				selectedAlphabetName);

		String keywordToFormat = editorKeywordView.getKeywordToEdit();
		String formatedKeyword = "";
		switch (selectedCipher) {
		case Global.VIGENERE:
			formatedKeyword = Vigenere.formatKeyword(keywordToFormat,
					selectedAlphabet);
			break;
		case Global.PLAYFAIR:
			formatedKeyword = Playfair.formatKeyword(keywordToFormat,
					selectedAlphabet);
			break;
		case Global.HILL:
			formatedKeyword = Hill.formatKeyword(keywordToFormat,
					selectedAlphabet);
			break;
		}
		editorKeywordView.setKeywordToEdit(formatedKeyword);
		if (!formatedKeyword.equals("")) {
			editorKeywordView.enableAddButton(true);
		} else {
			editorKeywordView.enableAddButton(false);
		}
	}

	private void addKeyword() {
		int cipher = editorKeywordView.getSelectedCipher();
		String keyword = editorKeywordView.getKeywordToEdit();
		String alphabetName = editorKeywordView.getSelectedAlphabetName();

		keywordDAO.create(cipher, alphabetName, keyword);
	}

	private void removeKeyword() {
		int cipher = editorKeywordView.getSelectedCipher();
		String keyword = editorKeywordView.getKeywordToEdit();
		String alphabetName = editorKeywordView.getSelectedAlphabetName();

		keywordDAO.delete(cipher, alphabetName, keyword);
	}
}
